/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"fmt"
	//"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
	
)

type SysAddgoldsController struct {
	BaseController
}

func (self *SysAddgoldsController) List() {
	self.Data["pageTitle"] = "GM添加金币记录"
	//self.Ctx.WriteString("MangeAdminController.List")
	
	//beego.ReadFromRequest(&self.Controller);
	
	

	self.display();
}



func (self *SysAddgoldsController) getAdmins() {

	page,limit := 1,30;
	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "status", 1)
	result, count := models.ManageAdminGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["login_name"] = v.LoginName
		row["real_name"] = v.RealName
		row["phone"] = v.Phone
		row["email"] = v.Email
		//row["role_ids"] = v.RoleIds
		row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
		row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
		list[k] = row
	}
	self.Data["users"] = list
	fmt.Println("成功", MSG_OK, count, list);
	//self.ajaxList("成功", MSG_OK, count, list)
}


func (self *SysAddgoldsController) Table() {
	//列表
	beego.ReadFromRequest(&self.Controller);
	fmt.Println("====================SysAddgoldsController Table======================222\n")
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	
	userID,err := self.GetInt64("userID");
	manager := self.GetString("manager");
	date1:= strings.TrimSpace(self.GetString("date1"));
	date2 := strings.TrimSpace(self.GetString("date2"));
	selectType,err := self.GetInt("selectType");
	fmt.Println(selectType,userID,manager,date1,date2,page,limit);
	fmt.Println("==========================================")
	
	//time1,err := time.ParseInLocation("2006-02-01",date1,time.Local);
	//time2,err := time.ParseInLocation("2006-02-01",date2,time.Local);

	//查询条件
	filters := make([]interface{}, 0)
	
	if userID != 0 {
		filters = append(filters, "user_id",userID)
	}

	if manager !="" {
		filters = append(filters, "manager_name",manager)
	}

	if selectType <=0 || selectType >3 { 
		selectType = 1
	}
	filters = append(filters,"gold_type",selectType)
	if date1 == "" && date2 == "" {

	}else{
		if date1 == "" {
			date1 = "2006-01-02 15:04:05"
		}
	
		if date2 == "" {
			date2 = "2019-01-02 15:04:05"
		}

		//filters = append(filters, "operation_time","2017-11-07 15:00:20","2017-11-07 17:00:20")
	}

	
	fmt.Println(filters);

	//result,count := models.GMAddRecordGetListSQL("SELECT id FROM GMAddRecord  LIMIT 1,1WHERE operation_time >=\"2017-11-00 12:40:05\" and operation_time<=\"2017-11-08 12:12:10\")
	result, count := models.GMAddRecordGetList(page, self.pageSize,date1,date2, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["userid"] = v.User_id
		row["nickname"] = v.Nickname
		row["admin"] = v.Manager_name
		row["time"] = v.Operation_time
		row["explain"] = v.Explain
		row["amount"] = v.Amount

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}
